package com.wn.confirm;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

/**
 * @author yellow Docter
 * @date 2023 -07-26
 * @desc
 * 消息的 确认 和回退机制
 */
//@Component
public class ConfirmCallBack implements RabbitTemplate.ConfirmCallback,RabbitTemplate.ReturnCallback {

    //到 交换机 中的  确认机制
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        System.out.println("回调的相关数据:correlationData = " + correlationData);
        System.out.println("是否确认收到消息:ack = " + ack);
        System.out.println("原因:cause = " + cause);

//        Message message = correlationData.getReturnedMessage();
//        String messageId = message.getMessageProperties().getMessageId();

        //把消息的id存储到redis中

        if (ack){  //true 消息 成功发送到 交换机中
            System.out.println("消息到交换机中... = ");
        }else{    //消息到交换机中失败
            System.out.println("消息到交换机中...失败 ");
            System.out.println("原因:cause = " + cause);
            //消息失败

            //存储到mysql中进行统一处理
            //重新发送一次
        }
    }

    // 交换机到  队列的回退机制
    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        System.out.println(" ========returnCallBack===========");
        System.out.println("回退消息:message = " + message);
        System.out.println("响应码:replyCode = " + replyCode);
        System.out.println("回退原因:replyText = " + replyText);
        System.out.println("交换机:exchange = " + exchange);
        System.out.println("路由:routingKey = " + routingKey);

    }
}
